﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Webcar.BLL;
using ToolsLayer;
using System.Data;

namespace Webcar.Sec
{
    public partial class SecReportWinterList : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GridViewStyler gvs = new GridViewStyler(this.GridView1);
            gvs.SetStyle();
            if (!this.IsPostBack)
            {
                this.SearchPanel1.BindColumn("winter_report.xml");
                this.SearchPanel1.OriginSql = this.GetSQL();
                this.SearchPanel1.SetQuery(this.GetQueryStringCondition());
                this.BindInfo();
                this.HyperLink1.NavigateUrl = this.AddReturnUrl("EditWinterReport.aspx");
            }
        }

        private string GetSQL()
        {
            string sql = @"select a.*,b.dept_name from sec_report_winter a,dept b where a.dept_code=b.dept_code and a.dept_code in (select dept_code from bs_man_dept where man_code='{0}')";
            
            return string.Format(sql, CurrentUser.LoginUserID);
        }
        private void BindInfo()
        {
            this.WebPager1.ItemsPerPage = 13; //每页显示的记录条数。
            if (this.SearchPanel1.GetSort() == "")
                this.WebPager1.SortField = "check_day";
            else
                this.WebPager1.SortField = this.SearchPanel1.GetSort(); //用于排序的字段。
            this.WebPager1.Sortmode = DevCenterNew.SortMode.DESC;
            if (this.SearchPanel1.GetSortMode().ToUpper() == "DESC")
                this.WebPager1.Sortmode = DevCenterNew.SortMode.DESC;
            else if (this.SearchPanel1.GetSortMode().ToUpper() == "ASC")
                this.WebPager1.Sortmode = DevCenterNew.SortMode.ASC;
            this.WebPager1.ControlToPaginate = "GridView1"; //与其关联需绑定的DataGrid或DataList控件ID。

            this.WebPager1.SelectCommand = this.SearchPanel1.GetQuerySql();
            this.WebPager1.ConnectionString = DB.ConnectString;
            this.WebPager1.DataBind();
        }

        protected void Seach_Click(object sender, EventArgs e)
        {
            this.SearchPanel1.OriginSql = this.GetSQL();
            this.BindInfo();
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex == -1)
                return;
            HyperLink link = (HyperLink)e.Row.FindControl("linkEdit");
            DataRowView row = e.Row.DataItem as DataRowView;
            link.NavigateUrl = this.AddReturnUrl("EditWinterReport.aspx?dept=" + row["dept_code"].ToString() + "&day=" + row["check_day"].ToString());
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string dept_code = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
            string ny = this.GridView1.DataKeys[e.RowIndex].Values[1].ToString();
            string sql = "delete from sec_report_winter where dept_code='" + dept_code + "' and check_day='" + ny + "'";
            DB.ExecuteNonQuery(sql);
            Response.Redirect("SecReportWinterList.aspx");
        }

        protected void linkPrint_Click(object sender, EventArgs e)
        {
            this.OpenPrintWindow(this.SearchPanel1.GetQuerySql(), "~/Sec/winter_report.xml");
        }
    }
}
